package com.iscas.tech.service.impl;

import com.iscas.tech.bo.MdType;
import com.iscas.tech.entity.AtMdbusyValue;
import com.iscas.tech.entity.AtScideman;
import com.iscas.tech.mapper.AtScidemanMapper;
import com.iscas.tech.param.PageParam;
import com.iscas.tech.service.AtMdbusyValueService;
import com.iscas.tech.service.AtScidemanService;
import com.iscas.tech.utils.DataUtil;
import com.iscas.tech.vo.AtScidemanQueryVo;
import com.iscas.tech.vo.MdWithTechNeeds;
import io.geekidea.springbootplus.framework.common.service.impl.BaseServiceImpl;
import io.geekidea.springbootplus.framework.pagination.Paging;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 科技需求表 服务实现类
 *
 * @author yangyi
 * @since 2020-10-26
 */
@Slf4j
@Service
public class AtScidemanServiceImpl extends BaseServiceImpl<AtScidemanMapper, AtScideman> implements AtScidemanService {

    @Autowired
    private AtScidemanMapper atScidemanMapper;
    @Autowired
    private AtMdbusyValueService mBusinessService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean saveAtScideman(MdWithTechNeeds param) throws Exception {
        if (atScidemanMapper.saveTechNeeds(param)) {
            if (param.getTagList() != null) {
                for (AtMdbusyValue business : DataUtil.getBusinessList(param.getId(), param.getCreateTitle(), param.getTagList())) {
                    if (!mBusinessService.saveAtMdbusyValue(business)) {
                        throw new Exception();
                    }
                }
            }
            return true;
        }
        return false;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateTechNeeds(MdWithTechNeeds param) throws Exception {
        //  修改基础信息
        if (!super.updateById(param.transform())) {
            throw new Exception();
        }
        //  删除业务模块关联信息
        mBusinessService.deleteByBsId(param.getId().longValue(), MdType.TECH_NEEDS.getType());
        //  新增业务模块关联信息
        if (param.getTagList() != null) {
            for (AtMdbusyValue business : DataUtil.getBusinessList(param.getId(), param.getCreateTitle(), param.getTagList())) {
                if (!mBusinessService.saveAtMdbusyValue(business)) {
                    throw new Exception();
                }
            }
        }
        return true;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteTechNeeds(String ids) throws Exception {
        if (ids == null) {
            return true;
        }
        String[] idList = ids.split(",");
        for (String id : idList) {
            if (!atScidemanMapper.deleteTechNeeds(Long.parseLong(id))) {
                throw new Exception();
            }
        }
        return true;
    }

    @Override
    public Paging<AtScidemanQueryVo> getTechNeedsPageList(PageParam pageParam, Integer audit) throws Exception {
        List<AtScidemanQueryVo> pageList = atScidemanMapper.getTechNeedsPageList(pageParam.getOffset(), pageParam.getPageSize(), audit);
        Paging paging = new Paging();
        paging.setRecords(pageList);
        paging.setTotal(atScidemanMapper.getTechNeedsCount(audit));
        return paging;
    }

    @Override
    public AtScidemanQueryVo get(Long id) {
        return atScidemanMapper.get(id);
    }

    @Override
    public Boolean checkTechNeeds(Long id) throws Exception {
        return atScidemanMapper.checkTechNeeds(id);
    }
}
